home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 334_03 / unixpc.trm < prev    next >
Text File  |  1991-02-04  |  15KB  |  536 lines

  1. /* GNUPLOT - unixpc.trm */
  2. /*
  3.  * Copyright (C) 1990   
  4.  *
  5.  * Permission to use, copy, and distribute this software and its
  6.  * documentation for any purpose with or without fee is hereby granted, 
  7.  * provided that the above copyright notice appear in all copies and 
  8.  * that both that copyright notice and this permission notice appear 
  9.  * in supporting documentation.
  10.  *
  11.  * Permission to modify the software is granted, but not the right to
  12.  * distribute the modified code.  Modifications are to be distributed 
  13.  * as patches to released version.
  14.  *  
  15.  * This software  is provided "as is" without express or implied warranty.
  16.  * 
  17.  * This file is included by ../term.c.
  18.  *
  19.  * This terminal driver supports:
  20.  *  Unix PC's (ATT 3b1)
  21.  *
  22.  * AUTHORS
  23.  *    John Campbell
  24.  * 
  25.  * send your comments or suggestions to (pixar!info-gnuplot@sun.com).
  26.  * 
  27.  */
  28.  
  29. /*
  30. >From: John Campbell (...!arizona!naucse!jdc)
  31.  
  32. I originally ported gnuplot to the ATT 3b1 (ATT7300) on 12/4/88, and then
  33. added the minimal code needed to bring it up to 2.0 level on 1/28/90.  The 
  34. 3b1, as I view it, is a 720x300 bitmapped, monochrome display (often people 
  35. don't use the top 12 scan lines and thus the effective size is 720x288).  I 
  36. tried to maximize the size of the graph area, by using these top 12 lines 
  37. (normally reserved) and set up a signal handler to restore them upon exit, 
  38. abort, etc.
  39.  
  40. Line styles were "fudged" (they do not know the aspect ratio).  The same
  41. line style may look different depending upon the slope of the curve.  Due to
  42. this only 4 line styles were implemented.  While more line types are possible,
  43. the current styles were chosen as distinguishable.
  44.  
  45. The 3b1 has 4 "special" rows at the bottom that I could not use in graphics
  46. mode.  It has been suggested that we use these lines for command prompting.
  47. Others have requested that we have a graphics window and a command window.
  48. My experience with gnuplot only includes relatively dumb graphics devices--
  49. hence gnuplot "looks and feels" normal to me the way I implemented it.
  50. I welcome either of these changes from someone else, however.
  51. */
  52.  
  53. #include <sys/window.h>   /* Started with tam.h--too much trouble. */
  54. #include <sys/signal.h>
  55. #include <errno.h>
  56.  
  57. #define uPC_HIGH_BIT    (0x8000)
  58.  
  59. typedef unsigned short Scr_type;
  60. typedef unsigned char Scr_kluge;
  61.  
  62. #define uPC_XMAX 720
  63. #define uPC_YMAX 300
  64.  
  65. #define uPC_XSIZE       45 /* Short ints. */
  66. #define uPC_YSIZE uPC_YMAX
  67.  
  68. Scr_type uPC_display[uPC_YSIZE][uPC_XSIZE];
  69. int uPC_width = 2*uPC_XSIZE;
  70. int uPC_sx=0, uPC_sy=0;
  71. int uPC_cur_linetype=0;
  72. int uPC_angle = 0;
  73. unsigned short uPC_raster_count=0;
  74. static Scr_type lookup[] = {
  75.       0x0001, 0x0002, 0x0004, 0x0008,
  76.       0x0010, 0x0020, 0x0040, 0x0080,
  77.       0x0100, 0x0200, 0x0400, 0x0800,
  78.       0x1000, 0x2000, 0x4000, 0x8000,
  79.    };
  80.  
  81. #define uPC_XLAST (uPC_XMAX - 1)
  82. #define uPC_YLAST (uPC_YMAX - 1)
  83.  
  84. #define uPC_VCHAR FNT5X9_VCHAR
  85. #define uPC_HCHAR FNT5X9_HCHAR
  86. #define uPC_VTIC   8
  87. #define uPC_HTIC  12
  88.  
  89. extern errno, sys_nerr;
  90. extern char *sys_errlist[];
  91.  
  92. static struct urdata uPC_ur = {(unsigned short *)uPC_display, 2*uPC_XSIZE, 0, 0,
  93.                            0, 0, 0, 0, uPC_XMAX, uPC_YMAX, SRCSRC, DSTOR, 0};
  94.  
  95. #define IfErrOut(e1,e2,s1,s2) if (e1 e2) {\
  96. fprintf(stderr, "%s:: %s %s\n", sys_errlist[errno], s1, s2);\
  97. uPC_fixwind(0);\
  98. exit(-1);}
  99.  
  100. uPC_init()
  101. {
  102. /* This routine will ioctl to change 0 size */
  103.    int i;
  104.    struct uwdata uw;
  105.    int uPC_fixwind();
  106.    short gw;
  107.  
  108. /* Check that we are on the bitmapped window. */
  109.    if (iswind() != 0) {
  110.       fprintf (stderr, "Sorry--must run from the bitmapped terminal\n");
  111.       exit(-1);
  112.    }
  113.    for (i=1; i<=16; i++) {
  114.       if (i != SIGINT && i != SIGFPE)  /* Two are caught in plot.c */
  115.          signal (i, uPC_fixwind);
  116.    }
  117.  
  118. /* Increase the screen size */
  119.    uw.uw_x = 0;
  120.    uw.uw_y = 0;   /* Leave room for top status line. */
  121.    uw.uw_width = uPC_XMAX;      /* 720 */
  122.    uw.uw_height = uPC_YMAX;     /* 288 normal--we clobber 12 (top row)*/
  123.    uw.uw_uflags = 1;         /* Creates with no border */
  124.  
  125.    IfErrOut (ioctl(1, WIOCSETD, &uw), <0, "ioctl failed on", "WIOCSETD");
  126. }
  127.  
  128.  
  129. uPC_graphics()
  130. {
  131. /* This routine will clear the uPC_display buffer and window. */
  132.    register Scr_type *j;
  133.    register int i;
  134.  
  135.    j = (Scr_type *)uPC_display;
  136.    i = uPC_YSIZE*uPC_XSIZE + 1;
  137.  
  138.    while (--i)
  139.       *j++ = 0;
  140. /*
  141.    Position the cursor to the bottom of the screen so when we come back to
  142.    text mode we are just below the graph.
  143. */
  144.    printf ("\033[25;1H");
  145.  
  146.    uPC_ur.ur_dstop = DSTSRC;   /* replace (clear screen). */
  147.    IfErrOut (ioctl(1, WIOCRASTOP, &uPC_ur), <0,
  148.       "ioctl failed", "WIOCRASTOP");
  149.    uPC_ur.ur_dstop = DSTOR;   /* Or in (show text) */
  150. }
  151.  
  152.  
  153. uPC_text()
  154. {
  155. /* This routine will flush the display. */
  156.  
  157.    IfErrOut (ioctl(1, WIOCRASTOP, &uPC_ur), <0,
  158.       "ioctl failed", "WIOCRASTOP");
  159. }
  160.  
  161.  
  162. uPC_linetype(linetype)
  163. int linetype;
  164. {
  165. /* This routine records the current linetype. */
  166.    if (uPC_cur_linetype != linetype) {
  167.       uPC_raster_count = 0;
  168.       uPC_cur_linetype = linetype;
  169.    }
  170. }
  171.  
  172.  
  173. uPC_move(x,y)
  174. unsigned int x,y;
  175. {
  176. /* This routine just records x and y in uPC_sx, uPC_sy */
  177.    uPC_sx = x;
  178.    uPC_sy = y;
  179. }
  180.  
  181.  
  182. /* Was just (*(a)|=(b)) */
  183. #define uPC_PLOT(a,b)   (uPC_cur_linetype != 0 ? uPC_plot_word (a,b) :\
  184.                                 *(a)|=(b))
  185.  
  186. uPC_plot_word(a,b)
  187. Scr_type *a, b;
  188. /*
  189.    Weak attempt to make line styles.  The real problem is the aspect
  190.    ratio.  This routine is called only when a bit is to be turned on in
  191.    a horizontal word.  A better line style routine would know something
  192.    about the slope of the line around the current point (in order to
  193.    change weighting).
  194.  
  195.    This yields 3 working linetypes plus a usable axis line type.
  196. */
  197. {
  198. /* Various line types */
  199.    switch (uPC_cur_linetype) {
  200.    case -1:
  201.    /* Distinguish between horizontal and vertical axis. */
  202.       if (uPC_sx > uPC_XMAX/8 && uPC_sx < 7*uPC_XMAX/8) {
  203.       /* Fuzzy tolerance because we don't know exactly where the y axis is */
  204.          if (++uPC_raster_count % 2 == 0) *(a) |= b;
  205.       }
  206.       else {
  207.       /* Due to aspect ratio, take every other y pixel and every third x. */
  208.          *(a) |= (b & 0x9999);
  209.       }
  210.    break;
  211.    case 1:
  212.    case 5:
  213.    /* Make a |    |----|    |----| type of line. */
  214.       if ((1<<uPC_raster_count) & 0xF0F0) *(a) |= b;
  215.       if (++uPC_raster_count > 15) uPC_raster_count = 0;
  216.    break;
  217.    case 2:
  218.    case 6:
  219.    /* Make a |----|----|----|--- |    | type of line. */
  220.       if ((1<<uPC_raster_count) & 0x0EFFF) *(a) |= b;
  221.       if (++uPC_raster_count > 19) uPC_raster_count = 0;
  222.    break;
  223.    case 3:
  224.    case 7:
  225.    /* Make a | -  | -  | -  | -  | type of line. */
  226.       if ((1<<uPC_raster_count) & 0x4444) *(a) |= b;
  227.       if (++uPC_raster_count > 15) uPC_raster_count = 0;
  228.    break;
  229.    case 4:
  230.    case 8:
  231.    default:
  232.       *(a) |= b;
  233.    break;
  234.    }
  235. }
  236.  
  237. uPC_vector(x,y)
  238. unsigned int x,y;
  239. {
  240. /* This routine calls line with x,y */
  241.    int x1 = uPC_sx, y1=uPC_sy, x2 = x, y2 = y;
  242.    register int  c, e, dx, dy, width;
  243.    register Scr_type mask, *a;
  244.  
  245. /* Record new sx, sy for next call to the vector routine. */
  246.    uPC_sx = x2;
  247.    uPC_sy = y2;
  248.  
  249.    a = &uPC_display[(uPC_YSIZE - 1) - y1][x1 >> 4];
  250.    mask = lookup[x1 & 0x0f];
  251.    width = uPC_width;
  252.  
  253.    if ((dx = x2 - x1) > 0) {
  254.       if ((dy = y2 - y1) > 0) {
  255.          if (dx > dy) {         /* dx > 0, dy > 0, dx > dy */
  256.             dy <<= 1;
  257.             e = dy - dx;
  258.             c = dx + 2;
  259.             dx <<= 1;
  260.  
  261.             while (--c) {
  262.                uPC_PLOT(a, mask);
  263.                if (e >= 0) {
  264.                   (Scr_kluge *)a -= width;
  265.                   e -= dx;
  266.                }
  267.                if (mask & uPC_HIGH_BIT) {
  268.                   mask = 1;
  269.                   a++;
  270.                } else
  271.                   mask <<= 1;
  272.                e += dy;
  273.             }
  274.          } else {            /* dx > 0, dy > 0, dx <= dy */
  275.             dx <<= 1;
  276.             e = dx - dy;
  277.             c = dy + 2;
  278.